#ifndef _ARRAY_C
#define _ARRAY_C

#include "_Array.h"

/* Constructor */
void
initArray(Array *initThis, size_t length) 
{
    void **arrayData = malloc(length * sizeof(void *));
    
    if(arrayData == NULL) {
        fprintf(stderr, 
                "%s:%d Error: malloc returned NULL\n", 
                __FILE__, 
                __LINE__);

        free(arrayData);
        return;
    }

    initThis->data = arrayData;
    initThis->length = length;
}

/* Destructor */
void
deleteArray(Array *deleteThis)
{
    if(deleteThis != NULL) {
        free(deleteThis->data);
    }
}

void *
ArrayGet(Array *a, size_t index)
{
    if(_boundsCheck(a, index)) {
        return a->data[index];
    } else {
        _indexError(index);
        return NULL;
    }
}

void *
ArraySet(Array *a, size_t index, void *setThis)
{
    void *toReturn = NULL;
    
    if(_boundsCheck(a, index)) {
        toReturn = a->data[index];
        a->data[index] = setThis;
    } else {
        _indexError(index);
    }

    return toReturn;
}

#endif
